﻿using Microsoft.EntityFrameworkCore.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KWT.DRG_DIP.Svc.qcRules
{
    /// <summary>
    /// 自定义表达式比较器
    /// </summary>
    public class CustomExpressionComparer : IEqualityComparer<Expression>
    {
        private readonly ExpressionEqualityComparer _baseComparer = ExpressionEqualityComparer.Instance;

        public bool Equals(Expression? x, Expression? y)
        {
            if (x is LambdaExpression lx && y is LambdaExpression ly)
            {
                // 自定义逻辑（如处理逻辑运算符交换律）
                if (lx.Body is BinaryExpression binX && ly.Body is BinaryExpression binY &&
                    (binX.NodeType == ExpressionType.AndAlso || binX.NodeType == ExpressionType.OrElse))
                {
                    return (_baseComparer.Equals(binX.Left, binY.Left) && _baseComparer.Equals(binX.Right, binY.Right)) ||
                           (_baseComparer.Equals(binX.Left, binY.Right) && _baseComparer.Equals(binX.Right, binY.Left));
                }
            }

            // 调用基类默认逻辑
            return _baseComparer.Equals(x, y);
        }

        public int GetHashCode(Expression obj) => _baseComparer.GetHashCode(obj);
    }

}
